home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n6.arc / GENERIC.C < prev    next >
C/C++ Source or Header  |  1991-01-21  |  11KB  |  282 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Generic.c
  4.  
  5.     PURPOSE: Generic template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         Windows can have several copies of your application running at the
  18.         same time.  The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22.  
  23. #include "windows.h"            /* required for all Windows applications */
  24. #include "generic.h"            /* specific to this program             */
  25.  
  26. HANDLE hInst;                /* current instance                 */
  27.  
  28. /****************************************************************************
  29.  
  30.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  31.  
  32.     PURPOSE: calls initialization function, processes message loop
  33.  
  34.     COMMENTS:
  35.  
  36.         Windows recognizes this function by name as the initial entry point 
  37.         for the program.  This function calls the application initialization 
  38.         routine, if no other instance of the program is running, and always 
  39.         calls the instance initialization routine.  It then executes a message 
  40.         retrieval and dispatch loop that is the top-level control structure 
  41.         for the remainder of execution.  The loop is terminated when a WM_QUIT 
  42.         message is received, at which time this function exits the application 
  43.         instance by returning the value passed by PostQuitMessage(). 
  44.  
  45.         If this function must abort before entering the message loop, it 
  46.         returns the conventional value NULL.  
  47.  
  48. ****************************************************************************/
  49.  
  50. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  51. HANDLE hInstance;                 /* current instance         */
  52. HANDLE hPrevInstance;                 /* previous instance         */
  53. LPSTR lpCmdLine;                 /* command line             */
  54. int nCmdShow;                     /* show-window type (open/icon) */
  55. {
  56.     MSG msg;                     /* message                 */
  57.  
  58.     if (!hPrevInstance)             /* Other instances of app running? */
  59.     if (!InitApplication(hInstance)) /* Initialize shared things */
  60.         return (FALSE);         /* Exits if unable to initialize     */
  61.  
  62.     /* Perform initializations that apply to a specific instance */
  63.  
  64.     if (!InitInstance(hInstance, nCmdShow))
  65.         return (FALSE);
  66.  
  67.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  68.  
  69.     while (GetMessage(&msg,       /* message structure                 */
  70.         NULL,           /* handle of window receiving the message */
  71.         NULL,           /* lowest message to examine             */
  72.         NULL))           /* highest message to examine         */
  73.     {
  74.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  75.     DispatchMessage(&msg);       /* Dispatches message to window         */
  76.     }
  77.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  78. }
  79.  
  80.  
  81. /****************************************************************************
  82.  
  83.     FUNCTION: InitApplication(HANDLE)
  84.  
  85.     PURPOSE: Initializes window data and registers window class
  86.  
  87.     COMMENTS:
  88.  
  89.         This function is called at initialization time only if no other 
  90.         instances of the application are running.  This function performs 
  91.         initialization tasks that can be done once for any number of running 
  92.         instances.  
  93.  
  94.         In this case, we initialize a window class by filling out a data 
  95.         structure of type WNDCLASS and calling the Windows RegisterClass() 
  96.         function.  Since all instances of this application use the same window 
  97.         class, we only need to do this when the first instance is initialized.  
  98.  
  99.  
  100. ****************************************************************************/
  101.  
  102. BOOL InitApplication(hInstance)
  103. HANDLE hInstance;                   /* current instance         */
  104. {
  105.     WNDCLASS  wc;
  106.  
  107.     /* Fill in window class structure with parameters that describe the       */
  108.     /* main window.                                                           */
  109.  
  110.     wc.style = NULL;                    /* Class style(s).                    */
  111.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  112.                                         /* windows of this class.             */
  113.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  114.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  115.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  116.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  117.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  118.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  119.     wc.lpszMenuName =  "GenericMenu";   /* Name of menu resource in .RC file. */
  120.     wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
  121.  
  122.     /* Register the window class and return success/failure code. */
  123.  
  124.     return (RegisterClass(&wc));
  125.  
  126. }
  127.  
  128.  
  129. /****************************************************************************
  130.  
  131.     FUNCTION:  InitInstance(HANDLE, int)
  132.  
  133.     PURPOSE:  Saves instance handle and creates main window
  134.  
  135.     COMMENTS:
  136.  
  137.         This function is called at initialization time for every instance of 
  138.         this application.  This function performs initialization tasks that 
  139.         cannot be shared by multiple instances.  
  140.  
  141.         In this case, we save the instance handle in a static variable and 
  142.         create and display the main program window.  
  143.         
  144. ****************************************************************************/
  145.  
  146. BOOL InitInstance(hInstance, nCmdShow)
  147.     HANDLE          hInstance;          /* Current instance identifier.       */
  148.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  149. {
  150.     HWND            hWnd;               /* Main window handle.                */
  151.  
  152.     /* Save the instance handle in static variable, which will be used in  */
  153.     /* many subsequence calls from this application to Windows.            */
  154.  
  155.     hInst = hInstance;
  156.  
  157.     /* Create a main window for this application instance.  */
  158.  
  159.     hWnd = CreateWindow(
  160.         "GenericWClass",                /* See RegisterClass() call.          */
  161.         "Generic Sample Application",   /* Text for window title bar.         */
  162.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  163.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  164.         CW_USEDEFAULT,                  /* Default vertical position.         */
  165.         CW_USEDEFAULT,                  /* Default width.                     */
  166.         CW_USEDEFAULT,                  /* Default height.                    */
  167.         NULL,                           /* Overlapped windows have no parent. */
  168.         NULL,                           /* Use the window class menu.         */
  169.         hInstance,                      /* This instance owns this window.    */
  170.         NULL                            /* Pointer not needed.                */
  171.     );
  172.  
  173.     /* If window could not be created, return "failure" */
  174.  
  175.     if (!hWnd)
  176.         return (FALSE);
  177.  
  178.     /* Make the window visible; update its client area; and return "success" */
  179.  
  180.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  181.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  182.     return (TRUE);               /* Returns the value from PostQuitMessage */
  183.  
  184. }
  185.  
  186. /****************************************************************************
  187.  
  188.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  189.  
  190.     PURPOSE:  Processes messages
  191.  
  192.     MESSAGES:
  193.  
  194.     WM_COMMAND    - application menu (About dialog box)
  195.     WM_DESTROY    - destroy window
  196.  
  197.     COMMENTS:
  198.  
  199.     To process the IDM_ABOUT message, call MakeProcInstance() to get the
  200.     current instance address of the About() function.  Then call Dialog
  201.     box which will create the box according to the information in your
  202.     generic.rc file and turn control over to the About() function.    When
  203.     it returns, free the intance address.
  204.  
  205. ****************************************************************************/
  206.  
  207. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  208. HWND hWnd;                  /* window handle             */
  209. unsigned message;              /* type of message             */
  210. WORD wParam;                  /* additional information         */
  211. LONG lParam;                  /* additional information         */
  212. {
  213.     FARPROC lpProcAbout;          /* pointer to the "About" function */
  214.  
  215.     switch (message) {
  216.     case WM_COMMAND:       /* message: command from application menu */
  217.         if (wParam == IDM_ABOUT) {
  218.         lpProcAbout = MakeProcInstance(About, hInst);
  219.  
  220.         DialogBox(hInst,         /* current instance         */
  221.             "AboutBox",             /* resource to use         */
  222.             hWnd,             /* parent handle         */
  223.             lpProcAbout);         /* About() instance address */
  224.  
  225.         FreeProcInstance(lpProcAbout);
  226.         break;
  227.         }
  228.         else                /* Lets Windows process it         */
  229.         return (DefWindowProc(hWnd, message, wParam, lParam));
  230.  
  231.     case WM_DESTROY:          /* message: window being destroyed */
  232.         PostQuitMessage(0);
  233.         break;
  234.  
  235.     default:              /* Passes it on if unproccessed    */
  236.         return (DefWindowProc(hWnd, message, wParam, lParam));
  237.     }
  238.     return (NULL);
  239. }
  240.  
  241.  
  242. /****************************************************************************
  243.  
  244.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  245.  
  246.     PURPOSE:  Processes messages for "About" dialog box
  247.  
  248.     MESSAGES:
  249.  
  250.     WM_INITDIALOG - initialize dialog box
  251.     WM_COMMAND    - Input received
  252.  
  253.     COMMENTS:
  254.  
  255.     No initialization is needed for this particular dialog box, but TRUE
  256.     must be returned to Windows.
  257.  
  258.     Wait for user to click on "Ok" button, then close the dialog box.
  259.  
  260. ****************************************************************************/
  261.  
  262. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  263. HWND hDlg;                                /* window handle of the dialog box */
  264. unsigned message;                         /* type of message                 */
  265. WORD wParam;                              /* message-specific information    */
  266. LONG lParam;
  267. {
  268.     switch (message) {
  269.     case WM_INITDIALOG:           /* message: initialize dialog box */
  270.         return (TRUE);
  271.  
  272.     case WM_COMMAND:              /* message: received a command */
  273.         if (wParam == IDOK                /* "OK" box selected?         */
  274.                 || wParam == IDCANCEL) {      /* System menu close command? */
  275.         EndDialog(hDlg, TRUE);          /* Exits the dialog box         */
  276.         return (TRUE);
  277.         }
  278.         break;
  279.     }
  280.     return (FALSE);                  /* Didn't process a message    */
  281. }
  282.